﻿

Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_CertifiedChamDut_Action
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Request.QueryString("Id") Is Nothing Then
                hidID.Value = Request.QueryString("Id")
                hidCertifiedId.Value = Request.QueryString("CertifiedID")
                BindToGrid(hidID.Value)
                'Load list action status
                LoadListActionStatus()
            End If
        End If
        Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
        If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
            ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
        Else
            Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
        End If
    End Sub
    'Show du lieu cua contest
    Sub BindToGrid(ByVal iAnnouncementId As Integer)
        Using data As New VSEntities
            Dim p = (From q In data.CertifiedChamDuts Join h In data.CertifiedHouseholds On q.CertifiedID Equals h.CertifiedID _
                                                       Where q.Id = iAnnouncementId Select New With {q.Id,
                                                                                                     h.Biz_VietName,
                                                                                                     q.NgayTao,
                                                                                                     q.StatusId,
                                                                                                     q.NgaySua}).ToList
            Dim strKey_Name() As String = {"Id", "Biz_VietName", "NgayTao", "StatusId"}
            With grdShow
                .DataKeyNames = strKey_Name
                .DataSource = p
                .DataBind()
            End With
        End Using
    End Sub

    Protected Sub grdShow_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdShow.RowDataBound
        If e.Row.RowIndex >= 0 Then
            Dim lblNgayTao As Label = CType(e.Row.FindControl("lblNgayTao"), Label)

            Dim strNgayTao As String = Convert.ToDateTime(grdShow.DataKeys(e.Row.RowIndex)("NgayTao")).ToString("dd/MM/yyyy")
            lblNgayTao.Text = strNgayTao

            Dim lblTrangThai As Label = CType(e.Row.FindControl("lblTrangThai"), Label)
            Dim _StatusId As Integer = grdShow.DataKeys(e.Row.RowIndex)("StatusId").ToString
            Using data As New VSEntities
                Dim p = (From q In data.Status Where q.StatusId = _StatusId And q.FunctionId = Function_Name.CertifiedChamDut Select q).FirstOrDefault
                If Not p Is Nothing Then
                    lblTrangThai.Text = p.Title
                End If
            End Using
        End If
    End Sub
    Sub LoadListHour(ByVal ddlName As DropDownList)
        For i As Integer = 0 To 23
            Dim iTem As New ListItem(i.ToString(), i.ToString())
            ddlName.Items.Add(iTem)
        Next
    End Sub
    Sub LoadListMinutes(ByVal ddlName As DropDownList)
        For i As Integer = 0 To 59
            Dim iTem As New ListItem(i.ToString(), i.ToString())
            ddlName.Items.Add(iTem)
        Next
    End Sub
    'Load list action status of current status
    Sub LoadListActionStatus()



        Using Data As New VSEntities
            Dim iCurrentStatusId As Integer = (From q In Data.CertifiedChamDuts Where q.Id = hidID.Value Select q.StatusId).FirstOrDefault
            'Load action tuong ung voi current statusid & FunctionId
            Dim st = (From q In Data.StatusActions Where q.CurrentStatusId = iCurrentStatusId And q.FunctionId = Function_Name.CertifiedChamDut _
                      Select New With {q.CurrentStatusId, q.ActionId, q.Audit, .ActionTitle = q.Action.Title, .AuditNumber = q.Action.AuditNumber}).ToList()
            For Each i In st
                If HasPermission(Function_Name.CertifiedChamDut, Session("RoleID"), i.CurrentStatusId, i.AuditNumber) Then
                    Dim _item As New ListItem(i.ActionTitle, i.ActionId)
                    ddlAction.Items.Insert(0, _item)
                End If
            Next
            ddlAction.SelectedIndex = ddlAction.Items.IndexOf(ddlAction.Items.FindByValue(Request.QueryString("ActionId")))
        End Using

        ' neu ko co quyen thi redirect
        'If ddlAction.Items.Count = 0 Then
        '    Excute_Javascript("Alertbox('Hồ sơ đã được xử lý.');window.location ='../../Page/CertifiedChamDut/List.aspx';", Me.Page, True)
        'End If
    End Sub

    'Luu trang thai action cua contest
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Using data As New VSEntities
            Dim p = (From q In data.CertifiedChamDuts Where q.Id = hidID.Value Select q).FirstOrDefault
            Try
                p.StatusId = (From q In data.StatusActions Where q.CurrentStatusId = p.StatusId And q.ActionId = ddlAction.SelectedValue _
                              And q.FunctionId = Function_Name.CertifiedChamDut _
                                Select q.NextStatusId).FirstOrDefault

                data.SaveChanges()

                'Ghi vao history changed status
                Dim oHis As New VSModel.ContentActionHistory
                oHis.FunctionId = Function_Name.CertifiedChamDut
                oHis.ObjId = hidID.Value
                oHis.Creator = Session("Username")
                oHis.Created = Now
                'oHis.PublishedDate = p.PublishedDate 'StringToDateTime(txtPublisheddate.Text.Trim(), ddlbeHour.SelectedValue & ":" & ddlbeMinutes.SelectedValue)
                'oHis.eEffectiveDate = p.eEffectiveDate
                oHis.CurrentStatusId = Request.QueryString("CStatusId")
                oHis.ActionStatusId = ddlAction.SelectedValue
                oHis.Note = txtNote.Text.Trim()
                data.ContentActionHistories.AddObject(oHis)
                data.SaveChanges()

                Dim a = (From q In data.CertifiedChamDuts Join h In data.CertifiedHouseholds On q.CertifiedID Equals h.CertifiedID _
                                                   Join k In data.Status On q.StatusId Equals k.StatusId
                                                   Where q.Id = hidID.Value Select New With {q.Id,
                                                                                             h.CertifiedID,
                                                                                                 h.Biz_VietName,
                                                                                                 q.NgayTao,
                                                                                                 .StatusName = k.TitleHoSo,
                                                                                                 q.NgaySua}).FirstOrDefault

                Insert_App_Log("Thay đổi trạng thái tạm ngưng:" & a.Biz_VietName & "", Function_Name.CertifiedChamDut, Audit_Type.Edit, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("Alertbox('Thay đổi trạng trạng thái hồ sơ thành công.');window.location ='../../Page/CertifiedChamDut/Detail.aspx?Id=" & hidID.Value & "&CertifiedID=" & a.CertifiedID & "';", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Thay đổi trạng hồ sơ thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub

    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBack.Click
        Response.Redirect("List.aspx")
    End Sub
End Class
